package com.baibei.ebec.welfare.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baibei.ebec.home.R;
import com.baibei.model.WelfareRankInfo;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * Created by tang on 2018/5/7.
 */

public class WelfareRankingAdapter extends RecyclerView.Adapter<WelfareRankingAdapter.ViewHolder> implements View.OnClickListener {
//    private ArrayList<Map<String,String>> datas;
//    private ArrayList<JSONObject> datas;
    private int[] detailVisiable;
    private List<WelfareRankInfo> rankInfos;


    public WelfareRankingAdapter(List<WelfareRankInfo> rankInfos){
//        this.datas = datas;
        this.rankInfos = rankInfos;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        //创建一个map存储detail的显示情况
        detailVisiable = new int[10];
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_welfare_ranking, null);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.ivArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (detailVisiable[(int) view.getTag()] == 0){
                    viewHolder.llDetail.setVisibility(View.VISIBLE);
                    viewHolder.ivArrow.setImageResource(R.drawable.icon_arrow_up);
                    detailVisiable[(int) view.getTag()] = 1;
                    return;
                }
                viewHolder.llDetail.setVisibility(View.GONE);
                viewHolder.ivArrow.setImageResource(R.drawable.icon_arrow_down);
                detailVisiable[(int) view.getTag()] = 0;
            }
        });

//        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//            }
//        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
//        JSONObject job = datas.get(i);
        WelfareRankInfo rankInfo = rankInfos.get(i);
        if (i == 0){
            viewHolder.ivRank.setImageResource(R.drawable.rank1);
            viewHolder.ivRank.setVisibility(View.VISIBLE);
            viewHolder.tvRank.setVisibility(View.GONE);
        } else if (i == 1){
            viewHolder.ivRank.setImageResource(R.drawable.rank2);
            viewHolder.ivRank.setVisibility(View.VISIBLE);
            viewHolder.tvRank.setVisibility(View.GONE);
        } else if (i == 2){
            viewHolder.ivRank.setImageResource(R.drawable.rank3);
            viewHolder.ivRank.setVisibility(View.VISIBLE);
            viewHolder.tvRank.setVisibility(View.GONE);
        } else {
            viewHolder.tvRank.setText(rankInfo.getRanknum());
            viewHolder.ivRank.setVisibility(View.GONE);
            viewHolder.tvRank.setVisibility(View.VISIBLE);
        }
        viewHolder.tvUserPhone.setText(rankInfo.getUserName());
        viewHolder.tvProfitTitle.setText("+"+rankInfo.getPlamount());
        viewHolder.tvTradeItem.setText(rankInfo.getName());
        viewHolder.tvBuyPrice.setText("建仓价  "+rankInfo.getBuyprice());
        viewHolder.tvSellPrice.setText("平仓价  "+rankInfo.getSellprice());
        viewHolder.tvBuyTime.setText("建仓时间  "+ms2Date(rankInfo.getBuytime()));
        viewHolder.tvSellTime.setText("平仓时间  "+ms2Date(rankInfo.getSelltime()));
        viewHolder.tvTopLimit.setText("止盈  "+rankInfo.getToplimit()+"%");
        viewHolder.tvBottomLimit.setText("止损  "+rankInfo.getBottomlimit()+"%");
        viewHolder.tvFee.setText("手续费  "+rankInfo.getFee());
        viewHolder.ivUpOrDown.setImageResource("1".equals(rankInfo.getBuytype())?R.drawable.ic_down:R.drawable.ic_up);
        viewHolder.tvTicket.setText("代金券  "+("0".equals(rankInfo.getIsJuan())?"未用":"使用"));
        viewHolder.ivHandOrAuto.setImageResource("true".equals(rankInfo.getIsAuto())?R.drawable.icon_auto:R.drawable.icon_hand);
        viewHolder.tvWinRate.setText(rankInfo.getRate()+"%");
        viewHolder.tvProfitContent.setText("+"+rankInfo.getPlamount()+"元");

        viewHolder.ivArrow.setTag(i);
        viewHolder.itemView.setTag(i);
        detailVisiable[i] = 0;
    }

    @Override
    public int getItemCount() {
        return rankInfos.size();
    }

    public void setOnItemListener(MyItemClickListener listener){
        this.listener = listener;
    }

    @Override
    public void onClick(View view) {
        int position = (int) view.getTag();
        listener.onClick(view,position);
    }


    public interface MyItemClickListener{
        void onClick(View view,int position);
    }

    MyItemClickListener listener = null;


    public class ViewHolder extends RecyclerView.ViewHolder{

        private final ImageView ivRank;
        private final TextView tvRank;
        private final TextView tvUserPhone;
        private final TextView tvWinRate;
        private final TextView tvProfitTitle;
        private final ImageView ivArrow;
        private final LinearLayout llDetail;
        private final ImageView ivUpOrDown;
        private final TextView tvTradeItem;
        private final TextView tvProfitContent;
        private final ImageView ivHandOrAuto;
        private final TextView tvBuyPrice;
        private final TextView tvSellPrice;
        private final TextView tvFee;
        private final TextView tvBottomLimit;
        private final TextView tvBuyTime;
        private final TextView tvSellTime;
        private final TextView tvTicket;
        private final TextView tvTopLimit;

        public ViewHolder(View itemView) {
            super(itemView);
            ivRank = itemView.findViewById(R.id.iv_rank);
            tvRank = itemView.findViewById(R.id.tv_rank);
            tvUserPhone = itemView.findViewById(R.id.tv_user_phone);
            tvWinRate = itemView.findViewById(R.id.tv_winrate);
            tvProfitTitle = itemView.findViewById(R.id.tv_profit_title);
            ivArrow = itemView.findViewById(R.id.iv_arrow);
            llDetail = itemView.findViewById(R.id.ll_detail);
            ivUpOrDown = itemView.findViewById(R.id.iv_up_or_down);
            tvTradeItem = itemView.findViewById(R.id.tv_trade_item);
            tvProfitContent = itemView.findViewById(R.id.tv_profit_content);
            ivHandOrAuto = itemView.findViewById(R.id.iv_hand_or_auto);
            tvBuyPrice = itemView.findViewById(R.id.tv_buy_price);
            tvSellPrice = itemView.findViewById(R.id.tv_sell_price);
            tvFee = itemView.findViewById(R.id.tv_fee);
            tvBottomLimit = itemView.findViewById(R.id.tv_bottom_limit);
            tvBuyTime = itemView.findViewById(R.id.tv_buy_time);
            tvSellTime = itemView.findViewById(R.id.tv_sell_time);
            tvTicket = itemView.findViewById(R.id.tv_ticket);
            tvTopLimit = itemView.findViewById(R.id.tv_top_limit);

            ivArrow.setOnClickListener(WelfareRankingAdapter.this);
            itemView.setOnClickListener(WelfareRankingAdapter.this);
        }
    }

    public static String ms2Date(String time){
        long ms = Long.valueOf(time);
        Date date = new Date(ms);
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss", Locale.getDefault());
        return format.format(date);
    }
}
